//
//  DKUnauthorizeMainViewController.m
//  DanKU
//
//  Created by 吴斌 on 2023/9/26.
//

#import "DKUnauthorizeMainViewController.h"
#import "DKLoginViewController.h"
#import "DKChangePwdController.h"
#import "DKFeedBackController.h"
#import "FAQMainViewController.h"
#import "DKMainComplaintController.h"
#import "DKMainCustomerServiceController.h"
#import "DKMainBankCardController.h"
#import "DKTabBarViewController.h"
#import "DKAuthorizeHomeViewController.h"
#import "DKBaseNavigationController.h"
//#import "DKOrderRepayViewController.h"
#import "DKMainInfoAlertView.h"
#import "DKUnauthorizedMainHeadTableCell.h"
#import "DKUnauthorizedMainBillCell.h"
#import "DKUnathorizedFunctionCell.h"
#import "DKUnathorizedTableViewCell.h"
#import "DKHomeHttp.h"
//#import "DKUnauthorizedMainHeadView.h"
//#import "DkUnathorizedMainCenterView.h"
//#import "DKUnauthorizedMainBillView.h"
//#import "DKUnathorizedFunctionView.h"
//#import "DKUnathorizedSigleView.h"

#import "DKTabBarViewController.h"
#import "DKUnauthorizeProductViewController.h"
#import "DKOrderRepayViewController.h"
@interface DKUnauthorizeMainViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView * dk_UnauthorizeMainTableView;
@property (nonatomic, strong)DKOrderListModel * dk_OrderListMdl;
//@property (nonatomic, strong)DKUnauthorizedMainHeadView * dk_UnauthorizedMainHeadView;
//@property (nonatomic, strong)DkUnathorizedMainCenterView * dk_UnathorizedMainCenterView;
//@property (nonatomic, strong)DKUnauthorizedMainBillView * dk_UnauthorizedMainBillView;
//@property (nonatomic, strong)DKUnathorizedFunctionView * dk_UnathorizedFunctionView;
//@property (nonatomic, strong)DKUnathorizedSigleView * dk_UnathorizedBankCardSigleView;
//@property (nonatomic, strong)DKUnathorizedSigleView * dk_UnathorizedPrivacySigleView;
@end

@implementation DKUnauthorizeMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = DK_Hex(@"#FAFAFA");
    [self dk_addSubView];
    [self addRecognizer];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [DKPublicHttp apiUserBrhavRecordForAPI:@{} withPageName:DK_Mine withAction:DK_ActionEnter complete:^{
            
    } fail:^{
            
    }];
    
    [self requestOrderList];
}
- (void)dk_addSubView{
    self.dk_UnauthorizeMainTableView = [[UITableView alloc] init];
    self.dk_UnauthorizeMainTableView.backgroundColor = UIColor.clearColor;
    self.dk_UnauthorizeMainTableView.delegate = self;
    self.dk_UnauthorizeMainTableView.dataSource = self;
    self.dk_UnauthorizeMainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.dk_UnauthorizeMainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    self.dk_UnauthorizeMainTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.dk_UnauthorizeMainTableView];
    [self.dk_UnauthorizeMainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(@(-DK_TabBar_H));
    }];
}
- (void)addRecognizer{
    UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [self.view addGestureRecognizer:recognizer];
}
- (void)requestOrderList{//用户借款历史
    WeakSelf(self)
    NSDictionary *orderDic = @{@"pageNum":@"1",@"pageSize":@"30",@"type":@"3"};
    [DKPublicHttp apiGetOrderListForAPI:orderDic complete:^(DKOrderListModel * _Nonnull orderlist) {
        weakSelf.dk_OrderListMdl = orderlist;
        [weakSelf.dk_UnauthorizeMainTableView reloadData];
    }];
    //获取用户信息
    [DKHomeHttp apiGetUserBaseInfoForAPI:@{} complete:^{
        [weakSelf.dk_UnauthorizeMainTableView reloadData];
    }];
    //获取用户额度
    [DKHomeHttp apiGetUserCreditInfoForAPI:@{} complete:^{
        [weakSelf.dk_UnauthorizeMainTableView reloadData];
    }];
}
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    [self makeShowMainInfoAler];
}
- (void)makeShowMainInfoAler{
    [DKPublicHttp apiUserBrhavRecordForAPI:@{} withPageName:DK_Settings withAction:DK_ActionEnter complete:^{
            
    } fail:^{
            
    }];
    WeakSelf(self)
    [DKMainInfoAlertView dk_ShowMainInfoAlert:self.view Complete:^{
        [DKGlobalHelper loginOutApp];
        //返回登录页面
        DKLoginViewController * dk_LoginViewCtrl = [[DKLoginViewController alloc] init];
        UINavigationController *dk_LoginViewNavCtrl = [[UINavigationController alloc] initWithRootViewController:dk_LoginViewCtrl];
        ((AppDelegate*)([UIApplication sharedApplication].delegate)).window.rootViewController = dk_LoginViewNavCtrl;
    } withFeedBack:^{
        DKFeedBackController *dk_FeedBackCtrl = [[DKFeedBackController alloc] init];
        dk_FeedBackCtrl.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:dk_FeedBackCtrl animated:YES];
    } withChangePwd:^{
        DKChangePwdController *dk_ChangePwdCtrl = [[DKChangePwdController alloc] init];
        dk_ChangePwdCtrl.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:dk_ChangePwdCtrl animated:YES];
    } withPrivacyAgreement:^{//隐私协议
//                DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] init];
//                dk_WebViewCtrl.webUrl = [NSURL URLWithString:DK_PrivacyAgreement].absoluteString;
        DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] initWithUrl:DK_PrivacyAgreement withIsNavBar:YES];
        dk_WebViewCtrl.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:dk_WebViewCtrl animated:YES];
    }];
}
#pragma mark -- UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        return DK_IsWidthScale(378)+DK_SafeAreaTop;
    }else if (indexPath.row == 1){
        if(self.dk_OrderListMdl.orderRecordList.count){
            return DK_IsWidthScale(77)+DK_IsWidthScale(43)*self.dk_OrderListMdl.orderRecordList.count;
        }else{
            return DK_IsWidthScale(176);
        }
    }else if (indexPath.row == 2){
        return DK_IsWidthScale(102);
    }else if (indexPath.row == 3){
        return DK_IsWidthScale(70);
    }else{
        return DK_IsWidthScale(77);
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WeakSelf(self)
    if(indexPath.row == 0){
        DKUnauthorizedMainHeadTableCell *dk_UnauthorizedMainHeadTableCell = [DKUnauthorizedMainHeadTableCell unauthorizedMainHeadCell:tableView];
        dk_UnauthorizedMainHeadTableCell.CheckUserInfoBlock = ^{
            [weakSelf makeShowMainInfoAler];
        };
        dk_UnauthorizedMainHeadTableCell.CheckUserMainBlock = ^(NSString * _Nonnull value, BOOL isLeft) {
            if(isLeft){//跳转首页
                UIViewController * dk_TabBarViewCtrl = ((AppDelegate*)([UIApplication sharedApplication].delegate)).window.rootViewController;
                if([dk_TabBarViewCtrl isKindOfClass:[DKTabBarViewController class]]){
                    DKTabBarViewController * dkTabBarViewCtrl = (DKTabBarViewController *)dk_TabBarViewCtrl;
                    dkTabBarViewCtrl.selectedIndex = 0;
                    if([[DKGlobalHelper defaultManager].loanSetpInfoMdl.step isEqualToString:@"100"]){//已经成功认证
                        UIViewController *curNavSelectVC = dkTabBarViewCtrl.selectedViewController;
                        if([curNavSelectVC isKindOfClass:[DKBaseNavigationController class]]){
                            DKBaseNavigationController * curNavBaseSelectVC = (DKBaseNavigationController *)curNavSelectVC;
                            if ([curNavBaseSelectVC.viewControllers.firstObject isKindOfClass:[DKAuthorizeHomeViewController class]]) {
                                DKAuthorizeHomeViewController * dkCurViewCtrl = (DKAuthorizeHomeViewController *)curNavBaseSelectVC.viewControllers.firstObject;
                                [dkCurViewCtrl updataUserLimit:value];

                            }
                        }
                    }
                }
            }else{//订单列表待还款页面
                UIViewController *rootViewController = ((AppDelegate*)([UIApplication sharedApplication].delegate)).window.rootViewController;
                if ([rootViewController isKindOfClass:[DKTabBarViewController class]]) {
                   
                    DKTabBarViewController *tab = (DKTabBarViewController *)rootViewController;
                    tab.selectedIndex = 1;// 跳转待还款页面
                }
                if ([DKUnauthorizeProductViewController isInitialized]) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshTable" object:self];
                }else{
                    double delayInSeconds = 0.3;
                    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
                    dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshTable" object:self];
                    });
                }
            }
        };
        dk_UnauthorizedMainHeadTableCell.dk_OrderListMdl = self.dk_OrderListMdl;
        return dk_UnauthorizedMainHeadTableCell;
    }else if (indexPath.row == 1){
        DKUnauthorizedMainBillCell *dk_UnauthorizedMainBillCell = [DKUnauthorizedMainBillCell unauthorizedMainBillCell:tableView];
        dk_UnauthorizedMainBillCell.orderRecordList = self.dk_OrderListMdl.orderRecordList;
        dk_UnauthorizedMainBillCell.BillRecordBlock = ^(DKOrderRecordListModel * _Nonnull orderRecordListMdl){
            DKOrderRepayViewController *repay = [DKOrderRepayViewController new];
            repay.dateSouce = orderRecordListMdl;
            [weakSelf.navigationController pushViewController:repay animated:YES];
        };
        return dk_UnauthorizedMainBillCell;
    }else if (indexPath.row == 2){
        DKUnathorizedFunctionCell *dk_UnathorizedFunctionCell = [DKUnathorizedFunctionCell unauthorizedFunctionCell:tableView];
        dk_UnathorizedFunctionCell.ClickFunBlock = ^(UnathorizedSigleType unathorizedSigleType) {
            if(unathorizedSigleType == UnathorizedSigleTypeFAQ){
                FAQMainViewController *FAQMainViewCtrl = [[FAQMainViewController alloc] init];
                [weakSelf.navigationController pushViewController:FAQMainViewCtrl animated:YES];
            }else if (unathorizedSigleType == UnathorizedSigleTypeService){
                DKMainCustomerServiceController *dk_MainCustomerServiceCtrl = [[DKMainCustomerServiceController alloc] init];
                [weakSelf.navigationController pushViewController:dk_MainCustomerServiceCtrl animated:YES];
            }else if (unathorizedSigleType == UnathorizedSigleTypeSComplaint){
                DKMainComplaintController *dk_MainComplaintCtrl = [[DKMainComplaintController alloc] init];
                [weakSelf.navigationController pushViewController:dk_MainComplaintCtrl animated:YES];
            }
        };
        return dk_UnathorizedFunctionCell;
    }else if(indexPath.row == 3){
        DKUnathorizedTableViewCell *dk_UnathorizedTableViewCell = [DKUnathorizedTableViewCell unauthorizedCell:tableView];
        dk_UnathorizedTableViewCell.unathorizedType = UnathorizedTypeBankCard;
        return dk_UnathorizedTableViewCell;
    }else{
        DKUnathorizedTableViewCell *dk_UnathorizedTableViewCell = [DKUnathorizedTableViewCell unauthorizedCell:tableView];
        dk_UnathorizedTableViewCell.unathorizedType = UnathorizedTypePrivacy;
        return dk_UnathorizedTableViewCell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 3){
        DKMainBankCardController *dk_MainBankCardCtrl = [[DKMainBankCardController alloc] init];
        [self.navigationController pushViewController:dk_MainBankCardCtrl animated:YES];
    }else if(indexPath.row == 4){
        DKWebViewController * dk_WebViewCtrl = [[DKWebViewController alloc] initWithUrl:DK_PrivacyAgreement withIsNavBar:YES];
        dk_WebViewCtrl.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:dk_WebViewCtrl animated:YES];
    }
}
@end
